<?php
require_once('Kbs/Controller/Action.php');

class Admin_DbController extends Kbs_Controller_Action 
{
    public function indexAction() 
    {
        die('...');
    }

    public function backupAction()
    {
        $bootstrap = $this->getInvokeArg('bootstrap');
        $db = $bootstrap->getResource('Db');
        $config = $db->getConfig();
        $tables = $db->listTables();

        if ($tables) {

            $backupPath = PROJECT_ROOT . '/repository/backups/sql/' 
                        . $config['dbname'] . date("Y-m-d-H-i-s") . '.sql';

            $backupPath = str_replace('\\', '/', $backupPath);

            $sql = "/* \n"
                 . "Data : " . date("Y-m-d-H-i-s") . "\n"
                 . "*/ \n\n";

            $sql .= "SET FOREIGN_KEY_CHECKS=0;\n\n";

            foreach ($tables as $table) {
                
                $sql = "/* \n"
                     . "Table structure for table `$table` \n"
                     . "*/ \n\n";

                $sql .= "DROP TABLE IF EXISTS `$table`;\n\n";
                
                $tableInfo = $db->query("SHOW CREATE TABLE `$table`;")->fetch();
                $sql .= $tableInfo['Create Table'] . "\n\n";

                $tableData = $db->query("SELECT * FROM `$table`;")->fetchAll();
                if ($tableData) {
                    foreach ($tableData as $data) {
                        $sql .= "INSERT INTO `$table` VALUES ()";

                    }
                }

                var_dump($sql);die;
            }
        }

        die('finish');
    }


}